package io.renren.modules.store.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.exception.RRException;
import io.renren.common.utils.BeanJudgeUtil;
import io.renren.common.utils.DateUtils;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.LocalTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.store.entity.TStoreTimeIntervalEntity;
import io.renren.modules.store.service.TStoreTimeIntervalService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-01-25 02:05:20
 */
@RestController
@RequestMapping("store/tstoretimeinterval")
public class TStoreTimeIntervalController extends AbstractController {
    @Autowired
    private TStoreTimeIntervalService tStoreTimeIntervalService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("store:tstoretimeinterval:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        params.put("key",params.get("key"));
        PageUtils page = tStoreTimeIntervalService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{timeIntervalId}")
    @RequiresPermissions("store:tstoretimeinterval:info")
    public R info(@PathVariable("timeIntervalId") Long timeIntervalId){
		TStoreTimeIntervalEntity tStoreTimeInterval = tStoreTimeIntervalService.getById(timeIntervalId);

        return R.ok().put("tStoreTimeInterval", tStoreTimeInterval);
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("store:tstoretimeinterval:save")
    public R save(@RequestBody TStoreTimeIntervalEntity tStoreTimeInterval){
        LocalTime start = new LocalTime(tStoreTimeInterval.getStartTime());
        LocalTime end = new LocalTime(tStoreTimeInterval.getEndTime());
        if (start.isAfter(end)){
            throw new RRException("开始时段不能大于结束时段");
        }
        //判断是否有重复时段
        List<TStoreTimeIntervalEntity> list = tStoreTimeIntervalService.list(new QueryWrapper<TStoreTimeIntervalEntity>()
                .eq("seller_id", getSeller())
        );
        list.forEach(item -> {
            LocalTime startTime = new LocalTime(item.getStartTime());
            LocalTime endTime = new LocalTime(item.getEndTime());
            if (start.isAfter(startTime) && end.isBefore(endTime)){
                throw new RRException("时段存在冲突");
            }
            if (start.equals(startTime) || end.equals(endTime)){
                throw new RRException("时段存在冲突");
            }
            if (start.isBefore(endTime) && end.isAfter(startTime)){
                throw new RRException("时段存在冲突");
            }
        });
        TStoreTimeIntervalEntity one = tStoreTimeIntervalService.getOne(new QueryWrapper<TStoreTimeIntervalEntity>()
                .eq("seller_id", getSeller())
                .eq("time_interval_name", tStoreTimeInterval.getTimeIntervalName())
        );
        if (!BeanJudgeUtil.beanIsNull(one)){
            throw new RRException("消费时段已存在，请重新设置");
        }
        tStoreTimeInterval.setSellerId(getSeller());
        tStoreTimeInterval.setStartTime(tStoreTimeInterval.getStartTime());
        tStoreTimeInterval.setEndTime(tStoreTimeInterval.getEndTime());
        tStoreTimeIntervalService.save(tStoreTimeInterval);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("store:tstoretimeinterval:update")
    public R update(@RequestBody TStoreTimeIntervalEntity tStoreTimeInterval){
		tStoreTimeIntervalService.updateById(tStoreTimeInterval);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("store:tstoretimeinterval:delete")
    public R delete(@RequestBody Long[] timeIntervalIds){
		tStoreTimeIntervalService.removeByIds(Arrays.asList(timeIntervalIds));

        return R.ok();
    }

}
